package com.ly.controller;

import com.ly.pojo.FastDFSFile;
import com.ly.pojo.User;
import com.ly.service.UserService;
import com.ly.utils.FastDfsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/fastdfs")
@CrossOrigin
public class FastDfsController {
    @Autowired
    private UserService userService;

    @PostMapping("/uploadImage")
    public String uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        String ex = filename.substring(filename.lastIndexOf(".") + 1);
        FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(), file.getBytes(), ex);
        String[] strings = FastDfsUtil.upLoadFile(fastDFSFile);
        System.out.println(strings.length);
        String url = "";
        url = "http://121.40.90.131:8080/" + strings[0] + "/" + strings[1];
        return url;
    }

    @GetMapping("/updateUser")
    public int updateUser(String userid, String newName, String fileId) {
        int i = userService.updateUser(userid, newName, fileId);
        return i;
    }

    @GetMapping("/updatePassword")
    public int updatePassword(@RequestParam("userid") String id,@RequestParam("newPwd") String newPassword){
        int i = userService.updatePassword(id, newPassword);
        return i;
    }

}
